public class Solution592 {
    public String fractionAddition(String expression) {
        if (!expression.startsWith("-")){
            expression="+".concat(expression);
        }
        int cur=0;
        int[] now=new int[]{1,0,1};
        while (cur<expression.length()){
            int flag2=expression.charAt(cur)=='+'?1:-1;
            cur++;
            StringBuilder stringBuilder=new StringBuilder();
            while (expression.charAt(cur)!='/'){
                stringBuilder.append(expression.charAt(cur));
                cur++;
            }
            int son2=Integer.parseInt(stringBuilder.toString());
            stringBuilder=new StringBuilder();
            cur++;
            while (cur<expression.length() && expression.charAt(cur)!='+' && expression.charAt(cur)!='-'){
                stringBuilder.append(expression.charAt(cur));
                cur++;
            }
            int mother2=Integer.parseInt(stringBuilder.toString());
            now=cal(now,new int[]{mother2,son2,flag2});
        }
        StringBuilder ans=new StringBuilder();
        if (now[2]==-1 && now[1]!=0){
            ans.append("-");
        }
        ans.append(now[1]).append('/').append(now[0]);
        return ans.toString();
    }

    private int[] cal(int[] now, int[] ints) {
        int[] ans=new int[3];
        if (now[2]==ints[2]){
            ans[2]=now[2];
            int[] p=add(now,ints);
            ans[0]=p[0];
            ans[1]=p[1];
        }else{
            int[] p=sub(now,ints);
            ans[2]=p[2]==now[2]?1:-1;
            ans[0]=p[0];
            ans[1]=p[1];
        }
        return ans;
    }

    private int[] add(int[] now, int[] ints) {
        int p=lcm(now[0],ints[0]);
        int[] ans=new int[]{p,p/now[0]*now[1]+p/ints[0]*ints[1]};
        if (ans[1]!=0) {
            int q = gcd(ans[0], ans[1]);
            ans[0] /= q;
            ans[1] /= q;
        }else{
            ans[0]=1;
        }
        return ans;
    }

    private int[] sub(int[] now, int[] ints) {
        int p=lcm(now[0],ints[0]);
        int flag=p/now[0]*now[1]-p/ints[0]*ints[1]>=0?1:-1;
        int[] ans=new int[]{p,Math.abs(p/now[0]*now[1]-p/ints[0]*ints[1]),flag};
        if (ans[1]!=0) {
            int q = gcd(ans[0], ans[1]);
            ans[0] /= q;
            ans[1] /= q;
        }else{
            ans[0]=1;
        }
        return ans;
    }

    private int lcm(int a, int b) {
        return a*b/gcd(a,b);
    }

    private int gcd(int a, int b) {
        if (a<b){
            int t=a;
            a=b;
            b=t;
        }
        return a%b==0?b:gcd(b,a%b);
    }

    public static void main(String[] args) {
        System.out.println(new Solution592().fractionAddition("-1/2+1/2+1/3"));
    }
}
